package cn.stronglink.assetmanage.index.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.index.adapter.ItemMenuAdapter;
import cn.stronglink.assetmanage.index.base.BaseFragment;
import cn.stronglink.assetmanage.index.contract.MenuContract;
import cn.stronglink.assetmanage.index.entity.PromissionEntity;
import cn.stronglink.assetmanage.index.entity.TaskEntity;
import cn.stronglink.assetmanage.index.presenter.MenuPresenter;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;


/**
 * @author zww
 * 2018 2018/6/10 13:31
 */
public class MenuFragment extends BaseFragment<MenuContract.View, MenuPresenter> implements MenuContract.View, onRecyclerListener {
    private static volatile MenuFragment instance;
    @BindView(R.id.menu_recyclerView)
    RecyclerView recyclerView;
    private Unbinder unbinder;
    private ItemMenuAdapter itemMenuAdapter;
    private List<TaskEntity> datas;

    public synchronized static MenuFragment getInstance() {
        if (null == instance) {
            instance = new MenuFragment();
        }
        return instance;
    }

    public List<TaskEntity> getDatas() {
        return datas;
    }

    public void setDatas(List<TaskEntity> datas) {
        this.datas = datas;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        itemMenuAdapter = new ItemMenuAdapter(getContext());
        itemMenuAdapter.setOnItemClickListener(this);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(itemMenuAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected MenuPresenter createPresenter() {
        return new MenuPresenter();
    }

    @Override
    public void onItemClicked(View v, int postion) {
        PromissionEntity promissionEntity = new PromissionEntity();
        promissionEntity.setPosition(postion);
        EventBus.getDefault().post(promissionEntity);
        for (int i = 0; i < itemMenuAdapter.getItemCount(); i++) {
            if (postion != i) {
                if (itemMenuAdapter != null && itemMenuAdapter.maps != null &&
                        itemMenuAdapter.maps.get(i) != null && itemMenuAdapter.maps.get(i).imgModule != null) {
                    itemMenuAdapter.maps.get(i).imgModule.setTagEnable(false);
                }
            } else {
                if (itemMenuAdapter != null && itemMenuAdapter.maps != null &&
                        itemMenuAdapter.maps.get(i) != null && itemMenuAdapter.maps.get(i).imgModule != null) {
                    itemMenuAdapter.maps.get(postion).imgModule.setTagEnable(true);
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventTaskEntity(List<TaskEntity> tasks) {
        this.datas = tasks;
        updateList(datas);
    }

    private void updateList(List<TaskEntity> datas) {
        itemMenuAdapter.notifyDatasChanged(datas);
    }
}
